const db = require('../db/index')

//获取用户基本信息
exports.getUserInfo = (req, res) => {
    const sql = 'select userid,user_account,user_name,gender,user_image from hb_user where userid = ? '
    db.query(sql, req.user.userid, (err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 1) return res.cc('获取用户信息失败！')
        res.send({
            status: 200,
            msg: '获取用户信息成功！',
            data: results[0]
        })
    })
}

//更新用户信息
exports.updateUserInfo = (req, res) => {
    const sql = 'update hb_user set ? where userid = ?'
    db.query(sql, [req.body, req.user.userid], (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('更新用户信息失败！')
        res.cc('更新用户信息成功！', 200)
    })
}

// 重置用户密码
exports.updatepwd = (req, res) => {
    const { oldPassword, newPassword } = req.body;

    // 验证旧密码是否正确
    const selectSql = 'select password from hb_user where userid = ?';
    db.query(selectSql, req.user.userid, (err, selectResults) => {
        if (err) return res.cc(err);
        if (selectResults.length !== 1) return res.send({ status: 500, msg: '用户不存在！' });
        const password = selectResults[0].password;
        if (password !== oldPassword) return res.send({ status: 500, msg: '旧密码不正确！' });


        // 更新密码为新密码
        const updateSql = 'update hb_user set password = ? where userid = ?';
        db.query(updateSql, [newPassword, req.user.userid], (err2, updateResults) => {
            if (err2) return res.cc(err2);
            if (updateResults.affectedRows !== 1) return res.send({ status: 500, msg: '修改密码失败！' });
            if (newPassword === oldPassword) return res.send({ status: 500, msg: '不能与原密码相同！' })
            res.cc('修改成功！', 200);
        });
    });
}

//更换用户头像
exports.updateAvatar = (req, res) => {
    const sql = 'update hb_user set user_image = ? where userid = ?'
    db.query(sql, [req.body.image, req.user.userid], (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('头像更换失败！')
        res.cc('头像更换成功！', 200)
    })
}

//发布动态
exports.releaseDynamic = (req, res) => {
    const sql = 'INSERT INTO hb_active SET ?';
    // 构造要插入的对象
    const obj = { user_name: req.user.user_name, ...req.body };

    db.query(sql, obj, (err, results) => {
        if (err) {
            // 更具体的错误处理
            return res.send({ status: 500, msg: '发布动态失败：' + err.message });
        }
        // 检查是否成功插入了一行数据
        if (results.affectedRows !== 1) {
            return res.send({ status: 500, msg: '发布动态失败！' });
        }
        res.send({ status: 200, msg: '发布动态成功！' });
    });
};

//获取自己发布的动态信息
exports.getDynamicInfoByown = (req, res) => {
    const sql = `SELECT A.*, U.*
    FROM hb_active A
    LEFT JOIN hb_user U ON A.user_name = U.user_name
    WHERE U.user_name = ?`
    db.query(sql, req.user.user_name, (err, results) => {
        if (err) return res.cc(err)
        if (results.length === 0) return res.send({ status: 400, msg: '还没有发布动态！' })
        res.send({ status: 200, data: results })
    })
}

//获取发布的动态信息
exports.getDynamicInfoList = (req, res) => {
    const sql = `SELECT a.*, b.*
    FROM hb_active a
    JOIN (
        SELECT * FROM hb_user
    ) b ON a.user_name = b.user_name`
    db.query(sql, (err, results) => {
        if (err) return res.cc(err)
        if (results.length === 0) return res.send({ status: 400, msg: '还没有动态！' })
        res.send({ status: 200, data: results })
    })
}